<template>
  <div class="system-user-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="600" top="5vh">
      <el-form ref="formRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="100px">
       
          <el-input type="number" v-model.trim="state.ruleForm.bonus" placeholder="请输入" maxlength="16" >
             <template #prepend>
              <el-select v-model="state.ruleForm.type" placeholder="请选择" style="width: 115px">
                <el-option label="增加" value="ADD" />
                <el-option label="减少" value="REDUCE" />
              </el-select>
            </template>
            <template #append>积分</template>
          </el-input>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit" size="default" :loading="state.submit.loading">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { nextTick, reactive, ref } from 'vue';
import { readAllRole } from '/@/api/role';
import { Role, User } from '/@/models';
import { DialogType } from '/@/constants/dialog-type';
import { addOrReduceBonus } from '/@/api/bonus';
import { ElMessage } from 'element-plus';
import { computed } from 'vue';
import { createRequiredRule } from '/@/utils';
import { arrayToOptions } from '/@/utils/array-to-options';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const formRef = ref();
const state = reactive({
  id: '',
  ruleForm: {
    bonus: '', // 账户名称
    type: '', // 用户昵称
  },
  rules: {
    type: [createRequiredRule()],
    bonus: createRequiredRule(),
  },
  submit: {
    loading: false
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: ''
  }
});



// 打开弹窗
const openDialog = (row?: User) => {
  state.dialog.isShowDialog = true;
  state.dialog.title = '积分操作';
  state.id = row.id
 
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = async () => {
  await formRef.value.validate();
  try {
    state.submit.loading = true;
    const data = state.ruleForm

    await addOrReduceBonus(state.id, data);
    ElMessage.success('保存成功');
    closeDialog();
    emit('refresh');
  } catch (err) {
    console.log(err);
  }

  state.submit.loading = false;
};

// 暴露变量
defineExpose({
  openDialog,
  closeDialog
});
</script>
